﻿namespace HRM.Update.Process
{
    using HRM.Update.Internet;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Windows.Forms;
    using System.Xml.XPath;

    public class ProcessInitUpdate
    {
        private List<UpdateFile> list_0;
        private Thread thread_0;

        public event SendObjectEventHander SendObject;

        public event StatusButtonEventHander StatusButton;

        public event StatusStringEventHander StatusString;

        public void Close()
        {
            if (this.thread_0.IsAlive)
            {
                this.thread_0.Abort();
            }
        }

        private void method_0(object object_0)
        {
            SendObjectEventHander hander = this.SendObject;
            if (hander != null)
            {
                hander(object_0);
            }
        }

        private void method_1(bool bool_0)
        {
            StatusButtonEventHander hander = this.StatusButton;
            if (hander != null)
            {
                hander(bool_0);
            }
        }

        private void method_2(string string_0)
        {
            StatusStringEventHander hander = this.StatusString;
            if (hander != null)
            {
                hander(string_0);
            }
        }

        private bool method_3()
        {
            return HRM.Update.Internet.Internet.IsConnectedToInternet();
        }

        private void method_4()
        {
            string str = "Update/Application[@ID='" + Class0.smethod_2() + "']/NewUpdate[@ID='" + Class0.smethod_3() + "']";
            try
            {
                HttpWebRequest request = WebRequest.Create("http://update.fessoft.com/update.xml") as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    this.method_2("... Đang kết nối tới HRM.com.vn");
                    StreamReader textReader = new StreamReader(response.GetResponseStream());
                    XPathNavigator navigator = new XPathDocument(textReader).CreateNavigator();
                    Console.WriteLine(str);
                    this.method_2(".... Đang kiểm tra phi\x00ean bản.");
                    XPathNodeIterator iterator = navigator.Select(str);
                    this.list_0 = new List<UpdateFile>(iterator.Count);
                    while (iterator.MoveNext())
                    {
                        UpdateFile item = new UpdateFile();
                        foreach (XPathNavigator navigator2 in iterator.Current.SelectChildren(XPathNodeType.Element))
                        {
                            string name = navigator2.Name;
                            switch (name)
                            {
                                case null:
                                    break;

                                case "Version":
                                    item.Version = navigator2.Value;
                                    break;

                                case "SoftType":
                                    item.SoftType = navigator2.Value;
                                    break;

                                default:
                                    if (!(name == "UpdateLink"))
                                    {
                                        if (name == "ZipName")
                                        {
                                            item.ZipName = navigator2.Value;
                                        }
                                    }
                                    else
                                    {
                                        item.UpdateLink = navigator2.Value;
                                    }
                                    break;
                            }
                        }
                        iterator.Current.MoveToFirstAttribute();
                        item.Id = iterator.Current.Value;
                        this.list_0.Add(item);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                Application.Exit();
            }
        }

        private UpdateFile method_5()
        {
            UpdateFile file = null;
            foreach (UpdateFile file2 in this.list_0)
            {
                this.method_2("... Đang kiểm tra bản cập nhật.");
                Thread.Sleep(500);
                file = file2;
                this.method_2(".... Đ\x00e3 t\x00ecm thấy bản cập nhật.");
                Thread.Sleep(500);
            }
            return file;
        }

        private void method_6()
        {
            if (!this.method_3())
            {
                this.method_2("Kh\x00f4ng thể kết nối với Internet");
            }
            else
            {
                this.method_4();
                UpdateFile file = this.method_5();
                if (file == null)
                {
                    this.method_2("Kh\x00f4ng t\x00ecm thấy bản cập nhật n\x00e0o cho phi\x00ean bản n\x00e0y.");
                }
                else
                {
                    this.method_2("Bản cập nhật cho phi\x00ean bản n\x00e0y l\x00e0 " + file.Version);
                    this.method_0(file);
                    this.method_1(true);
                }
            }
        }

        public void Start()
        {
            this.thread_0 = new Thread(new ThreadStart(this.method_6));
            this.thread_0.Start();
        }

        public delegate void SendObjectEventHander(object o);

        public delegate void StatusButtonEventHander(bool isEnable);

        public delegate void StatusStringEventHander(string str);
    }
}

